package com.hongxin.nw.netty.model.analysis.info;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldAnalysis;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

//sq=0时 使用这个类
@Slf4j
@Setter
@Getter
public class DTmitna1{

    private String infoAdr;//信息对象地址
    private String bcr;//BCR二进制读数,4BYTE

    private int sq;
    private int gun;//枪号

    private int idx = 0;
    private ByteBuf byteBuf;

    private void subInfoadr() {
        if (sq == StaticValue.SQ1){
//            this.infoadr = substring(TypeIdentif.BYTE_INFO_ADDRESS.getByteLength());
//            this.infoadr = BinaryTools.converter(this.infoadr);
//            this.gun = String.valueOf(this.infoadr.charAt(0));
//            this.infoadr = String.valueOf(Integer.parseInt(this.infoadr.substring(1),16));

            int bytelength = TypeIdentif.BYTE_INFO_ADDRESS.getInt();
            this.infoAdr = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
            this.infoAdr = BinaryTools.converter(this.infoAdr);
            this.gun = Integer.parseInt(this.infoAdr.substring(0,1));
            this.infoAdr = String.valueOf(Integer.parseInt(this.infoAdr.substring(2),16));
            this.idx += bytelength;
        }
    }
    private void subBcr() {
//        this.bcr = substring(TypeIdentif.BYTE_BCR.getByteLength());
//        this.bcr = FieldAnalysis.analysisBin(this.bcr);

        int bytelength = TypeIdentif.BYTE_BCR.getInt();
        this.bcr = BinaryTools.subByteBuf(bytelength, idx, byteBuf);
        this.bcr = FieldAnalysis.analysisBin(this.bcr);
        this.idx += bytelength;
    }

    public int analysisStr(int idx, ByteBuf byteBuf, int sq, String infoadr, int gun) {
        this.idx = idx;
        this.byteBuf = byteBuf;
        this.sq = sq;
        this.infoAdr = infoadr;
        this.gun = gun;
        subInfoadr();
        subBcr();
        return this.idx;
    }

    @Override
    public String toString() {
        return "DTmitna1{" +
                "infoAdr='" + infoAdr + '\'' +
                ", bcr='" + bcr + '\'' +
                '}';
    }
}